using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Entities;
namespace DAL
{
    public class tbDoanhNghiepDAL
    {
        private SqlConnection cn = null;
        public tbDoanhNghiepDAL ()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbDoanhNghiep doanhnghiep)
        {
            SqlCommand cm = new SqlCommand("sp_DoanhNghiep_Insert", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenDoanhNghiep", doanhnghiep.TenDoanhNghiep);
            cm.Parameters.AddWithValue("@DiaChi", doanhnghiep.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", doanhnghiep.DienThoai);
            cm.ExecuteNonQuery();
        }
        public void Update(tbDoanhNghiep doanhnghiep)
        {
            SqlCommand cm = new SqlCommand("sp_DoanhNghiep_Update", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdDoanhNghiep", doanhnghiep.IdDoanhNghiep);
            cm.Parameters.AddWithValue("@TenDoanhNghiep", doanhnghiep.TenDoanhNghiep);
            cm.Parameters.AddWithValue("@DiaChi", doanhnghiep.DiaChi);
            cm.Parameters.AddWithValue("@DienThoai", doanhnghiep.DienThoai);
            cm.ExecuteNonQuery();
        }
        public void Delete(int Id)
        {
            SqlCommand cm = new SqlCommand("sp_DoanhNghiep_Delete", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdDoanhNghiep", Id);
            cm.ExecuteNonQuery();
        }
        public tbDoanhNghiepCollection Get(string Where, int PageIndex,
               int PageSize, string OrderBy, string OrderDirection, out int TotalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_DoanhNghiep_Get", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", PageIndex);
            cm.Parameters.AddWithValue("@PageSize", PageSize);
            cm.Parameters.AddWithValue("@OrderBy", OrderBy);
            cm.Parameters.AddWithValue("@OrderDirection", OrderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
            tbDoanhNghiepCollection doanhnghiepCollection = new tbDoanhNghiepCollection();
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read()) 
            {
                tbDoanhNghiep doanhnghiep = new tbDoanhNghiep();
                doanhnghiep.IdDoanhNghiep = int.Parse(dr.GetValue(0).ToString());
                doanhnghiep.TenDoanhNghiep = dr.GetValue(1).ToString();
                doanhnghiep.DiaChi = dr.GetValue(2).ToString();
                doanhnghiep.DienThoai = dr.GetValue(3).ToString();
                doanhnghiepCollection.Add(doanhnghiep);
            }
            dr.Close();
            TotalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return doanhnghiepCollection;
        }
    }
}
